﻿' Copyright © Microsoft Corporation.  All Rights Reserved.
' This code released under the terms of the 
' Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
' <copyright file="DownloadProgressBar.vb" company="Microsoft">
'     Copyright © Microsoft Corporation. All rights reserved.
' </copyright>
' <summary>Implements the download progress bar class</summary>
' <author>Microsoft Expression Encoder Team</author>

Imports Microsoft.VisualBasic
Imports System
Imports System.Windows
Imports System.Windows.Controls

Namespace ExpressionMediaPlayer
	''' <summary>
	''' This class represents a progress bar for downloading a media item. Extends ProgressBar 
	''' by allowing start offset indicating DownloadProgressOffset.
	''' </summary>
    <TemplatePart(Name:="DeterminateRoot", Type:=GetType(FrameworkElement))> _
 Public Class DownloadProgressBar
        Inherits ProgressBar
        ''' <summary>
        ''' String for the visual element of the progress bar.
        ''' </summary>
        Private Const DeterminateRoot As String = "DeterminateRoot"
        ''' <summary>
        ''' Visual element representing progress bar indicator.
        ''' </summary>
        Private m_determinateRoot As FrameworkElement
        ''' <summary>
        ''' Dependancy property for Download Offset.
        ''' </summary>
        Public Shared ReadOnly DownloadOffsetProperty As DependencyProperty = DependencyProperty.Register("DownloadOffset", GetType(Double), GetType(DownloadProgressBar), New PropertyMetadata(0.0, New PropertyChangedCallback(AddressOf DownloadProgressBar.OnDownloadProgressOffsetPropertyChanged)))
        ''' <summary>
        ''' Initializes a new instance of the DownloadProgressBar class.
        ''' </summary>
        Public Sub New()
        End Sub
        ''' <summary>
        ''' Gets or sets the download offset, ie. if jumped into the middle of the file, this is the offset
        ''' nearest to the iframe silvelright downloaded
        ''' </summary>
        <System.ComponentModel.Category("Common Properties")> _
        Public Property DownloadOffset() As Double
            Get
                Return CType(Me.GetValue(DownloadOffsetProperty), Double)
            End Get

            Set(ByVal value As Double)
                Me.SetValue(DownloadOffsetProperty, value)
            End Set
        End Property
        ''' <summary>
        ''' Overridden OnApplyTemplate, sets the DeterminateRoot member, which is the visual element for the progress indicator.
        ''' </summary>
        Public Overrides Sub OnApplyTemplate()
            MyBase.OnApplyTemplate()
            m_determinateRoot = TryCast(GetTemplateChild(DeterminateRoot), FrameworkElement)
        End Sub
        ''' <summary>
        ''' Update the visual for the offset.
        ''' </summary>
        ''' <param name="offset">Amount of offset (gets bounded between Minimum and Maximum).</param>
        Private Sub SetOffsetVisual(ByVal offset As Double)
            offset = Math.Max(Math.Min(offset, Maximum), Minimum)
            Dim left As Double = ((offset - Minimum) / (Maximum - Minimum)) * ActualWidth
            If m_determinateRoot IsNot Nothing Then
                m_determinateRoot.Margin = New Thickness(left, m_determinateRoot.Margin.Top, m_determinateRoot.Margin.Right, m_determinateRoot.Margin.Bottom)
            End If
        End Sub
        ''' <summary>
        ''' Called when "DownloadProgressOffset" is set.
        ''' </summary>
        ''' <param name="obj">DownloadProgressBar source.</param>
        ''' <param name="args">New value etc.</param>
        Protected Shared Sub OnDownloadProgressOffsetPropertyChanged(ByVal obj As DependencyObject, ByVal args As DependencyPropertyChangedEventArgs)
            Dim downloadProgressBar As DownloadProgressBar = TryCast(obj, DownloadProgressBar)
            If downloadProgressBar IsNot Nothing Then
                Dim downloadProgressOffset As Double = CType(args.NewValue, Double)
                If Double.IsNaN(downloadProgressOffset) OrElse downloadProgressOffset < 0.0 OrElse downloadProgressOffset > 100.0 Then
                    Throw New ArgumentException()
                End If
                downloadProgressBar.SetOffsetVisual(CType(args.NewValue, Double))
            End If
        End Sub
    End Class
End Namespace
